﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TextMIS
{
    public partial class frmPackages : Form
    {
        public frmPackages()
        {
            InitializeComponent();
        }
        sysopr sysdic = new sysopr();
        //sysopr sysdic = new sysopr(); //系统字典获取数据

        dbopr baseinfo = new dbopr();     
        //dbopr baseinfo = new dbopr();//基础信息 

        //registopr regopr = new registopr(); //注册相关信息
        

        string str_PackId = "";//套餐编号
        DataTable dt_PackItemRel = null;
        void DataBind()
        {
            Tr_PkgList.Nodes.Clear();

            TreeNode node = new TreeNode("体检套餐");
            DataTable dt_Pack = baseinfo.Get_PackList();  //套餐信息列表
            foreach (DataRow dr in dt_Pack.Rows)
            {
                string str_BranID = dr["Pack_id"].ToString().Trim();   //套餐编号
                TreeNode node1 = new TreeNode(dr["Pack_name"].ToString().Trim());
                node1.Tag = str_BranID;

                node.Nodes.Add(node1);
            }
            Tr_PkgList.Nodes.Add(node);
            Tr_PkgList.ExpandAll();
            //new Common.Common().AddImage(node);

            cb_Sex.DataSource = sysdic.GetDicInfo(1);//1性别
            cb_Sex.DisplayMember = "xmmc";
            cb_Sex.ValueMember = "bzdm";

            cb_BuisTyp.DataSource = sysdic.GetDicInfo(10);//10业务类型
            cb_BuisTyp.DisplayMember = "xmmc";
            cb_BuisTyp.ValueMember = "bzdm";
        }
        private void frmPackages_Load(object sender, EventArgs e)
        {
            //new Common.Common().AddImages(imageList1);
            Tr_PkgList.ImageList = imageList1;

            lv_PacItem.SmallImageList = imageList1;
            lv_PacItem.StateImageList = imageList1;
            lv_PacItem.LargeImageList = imageList1;

            DataBind();
        }
        //套餐列表树选择后可以修改相应信息
        private void Tr_PkgList_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (object.Equals(null, Tr_PkgList.SelectedNode.Tag)) return;

            str_PackId = Tr_PkgList.SelectedNode.Tag.ToString().Trim();
            this.Text = "体检套餐设置【" + Tr_PkgList.SelectedNode.Text.Trim() + "】";
            //套餐详情
            DataTable dt_packageinfo = baseinfo.Get_PackItem(str_PackId);
            // 选中的套餐信息显示出来
            if (dt_packageinfo.Rows.Count < 1) return;
            tb_PackID.Text = dt_packageinfo.Rows[0]["Pack_id"].ToString().Trim();
            tb_DispOrder.Text = dt_packageinfo.Rows[0]["Disp_order"].ToString().Trim();
            cb_Sex.SelectedValue = dt_packageinfo.Rows[0]["SexAttr"].ToString().Trim();
            tb_PackName.Text = dt_packageinfo.Rows[0]["Pack_name"].ToString().Trim();
            tb_PacknameS.Text = dt_packageinfo.Rows[0]["Pinyin_sam"].ToString().Trim();
            tb_PriceStand.Text = dt_packageinfo.Rows[0]["StandPrice"].ToString().Trim();
            tb_Price.Text = dt_packageinfo.Rows[0]["SalePrice"].ToString().Trim();
            try
            {
                tb_SalePre.Text = (Convert.ToDecimal(tb_Price.Text) / Convert.ToDecimal(tb_PriceStand.Text) * 100).ToString("0.00");
            }
            catch
            {

            }
            cb_BuisTyp.SelectedValue = dt_packageinfo.Rows[0]["BuisType"].ToString().Trim();
            tb_Memo.Text = dt_packageinfo.Rows[0]["Pack_memo"].ToString().Trim();

            lv_PacItem.Items.Clear();
            lv_PacItem.View = View.Tile;
            // listview中显示套餐中包含的组合项目
            dt_PackItemRel = baseinfo.Get_PackItemRel(str_PackId);
            foreach (DataRow dr in dt_PackItemRel.Rows)
            {
                ListViewItem item = new ListViewItem();
                item.Tag = dr["Grp_id"].ToString().Trim();
                item.Text = dr["Gro_name"].ToString().Trim();
                item.ImageIndex = 4;
                lv_PacItem.Items.Add(item);
            }
            lv_PacItem.SmallImageList = imageList1;
            lv_PacItem.StateImageList = imageList1;
            lv_PacItem.LargeImageList = imageList1;

            if (Tr_PkgList.SelectedNode.Nodes.Count == 0)
            {
                Tr_PkgList.SelectedNode.SelectedImageIndex = Tr_PkgList.SelectedNode.ImageIndex;
            }
        }
        //折扣率变化时 价格也要随之变化
        private void tb_SalePre_TextChanged(object sender, EventArgs e)
        {
            try
            {
                tb_Price.Text = Convert.ToString(Convert.ToDecimal(tb_PriceStand.Text) * Convert.ToDecimal(tb_SalePre.Text) / 100);
            }
            catch
            {

            }
        }

        private void 清除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lv_PacItem.Items.Clear();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                lv_PacItem.Items.Remove(lv_PacItem.SelectedItems[0]);
                Get_Bzjg(lv_PacItem);
            }
            catch
            {

            }
        }

        private void bt_delete_Click(object sender, EventArgs e)
        {
            if (str_PackId == "") return;
            if (DialogResult.Yes == MessageBox.Show("是否真的删除？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1))
            {
                //dbopr ywszbiz1 = new dbopr();
                baseinfo.str_Delete_PackList(str_PackId);
                baseinfo.str_Delete_PackItem(str_PackId);
                baseinfo.Exec_ArryList();
                MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Tr_PkgList.Nodes.Remove(Tr_PkgList.SelectedNode);
                ClearControl();
            }
        }

        private void bt_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void ClearControl()
        {
            tb_PackID.Text = "";
            tb_DispOrder.Text = "";
            cb_Sex.SelectedIndex = -1;
            tb_PackName.Text = "";
            tb_PacknameS.Text = "";
            tb_PriceStand.Text = "0.00";
            tb_SalePre.Text = "100";
            tb_Price.Text = "0.00";
            cb_BuisTyp.SelectedIndex = 0;
            tb_Memo.Text = "";
            lv_PacItem.Items.Clear();
            str_PackId = "";
        }
        private void bt_save_Click(object sender, EventArgs e)
        {
            if (object.Equals(null, cb_Sex.SelectedValue))
            {
                MessageBox.Show("请选择性别！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ActiveControl = cb_Sex;
                return;
            }
            if (tb_PackName.Text.Trim() == "")
            {
                MessageBox.Show("请填写套餐名称！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ActiveControl = tb_PackName;
                return;
            }
            if (object.Equals(null, cb_BuisTyp.SelectedValue))
            {
                MessageBox.Show("请选择业务类型！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ActiveControl = cb_BuisTyp;
                return;
            }

            if (lv_PacItem.Items.Count <= 0)
            {
                MessageBox.Show("体检套餐项目不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //this.ActiveControl = cmb_tjywlx;
                return;
            }

            if (tb_PackID.Text.Trim() == "")
            {
                str_PackId = sysdic.GetId("tcid", 1);
                tb_PackID.Text = str_PackId;
            }
            //dbopr ywszbiz1 = new dbopr();
            //1先删除组合详情
            baseinfo.str_Delete_PackList(str_PackId);
            //2插入新组合详情
            baseinfo.str_Insert_PackItem(tb_PackID.Text.Trim(), tb_DispOrder.Text.Trim(), cb_Sex.SelectedValue.ToString().Trim(), tb_PackName.Text.Trim(),
                tb_PacknameS.Text.Trim(), tb_PriceStand.Text.Trim(), tb_Price.Text.Trim(), cb_BuisTyp.SelectedValue.ToString().Trim(), tb_Memo.Text.Trim());
            //3组合项目关系也要先删除在添加，要把flag加上
            foreach (ListViewItem item in lv_PacItem.Items)
            {
                baseinfo.str_Insert_PackItemRel(str_PackId, item.Tag.ToString().Trim());
            }
            baseinfo.Exec_ArryList();
            MessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            DataBind();
        }

        private void bt_add_Click(object sender, EventArgs e)
        {
            if (str_PackId == "") return;
            ClearControl();
            dt_PackItemRel.Rows.Clear();
        }

        private void 增加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (object.Equals(dt_PackItemRel, null))
            {
                dt_PackItemRel = baseinfo.Get_PackItemRel("0");
            }
            if (cb_Sex.SelectedValue == null)
            {
                return;
            }
            frmGrpSelect frm = new frmGrpSelect(dt_PackItemRel, cb_Sex.SelectedValue.ToString().Trim());
            if (frm.ShowDialog() == DialogResult.OK)
            {
                lv_PacItem.Items.Clear();
                lv_PacItem.View = View.Tile;
                dt_PackItemRel = frm.dt_PackItemRel;
                foreach (DataRow dr in dt_PackItemRel.Rows)
                {
                    ListViewItem item = new ListViewItem();
                    item.Tag = dr["Grp_id"].ToString().Trim();
                    item.Text = dr["Gro_name"].ToString().Trim();
                    item.ImageIndex = 4;
                    lv_PacItem.Items.Add(item);
                }
                Get_Bzjg(lv_PacItem);
            }

        }


        private void Get_Bzjg(ListView lv)//获取标准价格
        {
            string str_sql = "(";
            foreach (ListViewItem item in lv.Items)
            {
                str_sql = str_sql + "'" + item.Tag.ToString().Trim() + "',";
            }
            str_sql = str_sql.Substring(0, str_sql.Length - 1);
            if (str_sql.Length > 0) str_sql = str_sql + ")";
            if (str_sql == "")
                tb_PriceStand.Text = "0.00";
            else
                tb_PriceStand.Text = baseinfo.Get_ZhxmJg(str_sql);
            try
            {
                tb_Price.Text = (Convert.ToDecimal(tb_PriceStand.Text) * Convert.ToDecimal(tb_SalePre.Text) / 100).ToString("0.00");
            }
            catch
            {

            }
        }

        private void txt_disp_order_Leave(object sender, EventArgs e)
        {
            //tb_DispOrder.Text = new Common.Common().CharConverter(tb_DispOrder.Text.Trim());
        }

    }
}
